<template>
	<view>
		<uv-notify ref="notify" customStyle="z-index:9999"></uv-notify>
		<view class="set-threshold">
			 
		  <h1 class="title">设置全部单车光照阈值</h1>
		  <form @submit.prevent="setThreshold" class="form">
		    <label for="lightThreshold" class="label">光照阈值:</label>
		    <uv-input placeholder="请输入数字" border="surround" v-model="lightThreshold" class="input" clearable ></uv-input>
		    <uv-tags text="同步时间到设备" plain size="large" icon="tags" color="#00aa00"
		    	borderColor="#00aa00" class="label"></uv-tags>
		    <uv-switch v-model="isSyncTime" class="label"></uv-switch>
			<button type="submit" class="button" @click="setAllBikes()">发送设置</button>
		  </form>
		</view>
	</view>
	
</template>

<script>
export default {
  data() {
    return {
      lightThreshold: 0,
	  isSyncTime: false
    };
  },
  mounted() {
  	this.apiUrl = getApp().globalData.apiUrl;
  	this.user = uni.getStorageSync('user');
  	// setToken是设置header放入token的函数
  	this.setToken = getApp().globalData.setToken;
  	this.header = this.setToken();
  	/* 如果没有用户信息或者无token直接返回登录重新获取 */
  	if (this.user == '' || this.setToken == null) {
  		this.promptFail("请先登录！");
  		setTimeout(function() {
  			uni.navigateTo({
  				url: '/pages/user/login'
  			});
  		}, 1000);
  	}
  },
  methods: {
	/* 成功提示 */
	promptSuccess(msg) {
		this.$refs.notify.success(msg);
	},
	promptFail(msg) {
		this.$refs.notify.error(msg);
	},
    setAllBikes() {
		const backendURL = this.apiUrl + "/bicycle/setAllBikes";
		let header = this.setToken();
		let setAllBikesDto = {
			lightThreshold: this.lightThreshold,
			isSyncTime: this.isSyncTime
		}
		let satoken = header.satoken;
		uni.request({
			url: backendURL,
			method: 'POST',
			dataType: 'json',
			data: JSON.stringify(setAllBikesDto), // 将user对象转换为JSON字符串
			header: {
				'content-type': 'application/json', // 设置content-type为application/json
				'satoken': satoken
			},
			success: (res) => {
				console.log('成功', res);
				console.log(res.data);
				if (res.data.code == 200) {
					this.promptSuccess(res.data.msg);
				}
				if (res.data.code == 500) {
					this.promptFail(res.data.msg);
				}
			},
			fail: function(res) {
				console.log('错误', res);
				this.promptFail(res.msg);
			}
		});
    }
  }
};
</script>

<style scoped>
.set-threshold {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  text-align: center;
}

.title {
  font-size: 24px;
  margin-bottom: 20px;
}

.form {
  display: flex;
  flex-direction: column;
}

.label {
  font-size: 16px;
  margin-bottom: 5px;
}

.input {
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
  margin-bottom: 10px;
}

.button {
  background-color: #007bff;
  color: #fff;
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}

.button:hover {
  background-color: #0056b3;
}
</style>
